package com.zmn.plat.admin.controller.plat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.zmn.common.dto.KvDTO;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.common.dictionary.PlatEnum;
import com.zmn.plat.model.entity.plat.servcategory.PlatServCategory;
import com.zmn.plat.model.entity.plat.servcategory.log.PlatServCategoryLog;
import com.zmn.plat.model.entity.plat.servcategory.log.PlatServCategoryLogQuery;
import com.zmn.plat.services.interfaces.plat.servcategory.PlatServCategoryService;
import com.zmn.plat.services.interfaces.plat.servcategory.log.PlatServcategoryLogService;

@Controller
@RequestMapping("/plat/serv/category/")
public class PlatServCategoryListController extends PlatServCategoryBaseController {

    /**
     * The Service category service.
     */
    @Autowired
    protected PlatServCategoryService platServCategoryService;

    @Autowired
    PlatServcategoryLogService logService;

    /**
     * 开通分类
     *
     * @param plat
     *            平台
     * @return the model and view
     * @author linfeng
     * @since 2020/04/13
     */
    @RequestMapping(value = "servCategory")
    @RequiresPermissions("/plat/serv/category/servCategory.action")
    @SuppressWarnings("Duplicates")
    public ModelAndView servCategory(Integer plat) {
        ModelAndView mav = new ModelAndView("plat/servcategory/platServCategory");
        if (plat == null || plat == 0) {
            plat = PlatEnum.ZMN.value;
        }

        // 根据开通权限过滤默认展示的tab
        plat = getRealShowPlat(plat);

        List<KvDTO<Integer, String>> servCategorys = Collections.emptyList();
        List<PlatServCategoryLog> logList = new ArrayList<>();
        if (!NumberUtil.isNullOrZero(plat)) {
            servCategorys = super.getAllChildCategory((categoryQuery) -> categoryQuery.setShowType(ProductConsts.ERP_PRODUCT_TYPE));
            // 只显示平台开通分类最近50条操作日志
            PlatServCategoryLogQuery logQuery = new PlatServCategoryLogQuery();
            logQuery.setPlat(plat);
            logQuery.setPageIndex(0);
            logQuery.setPageSize(50);
            logList = logService.listPageByQuery(logQuery);
        }

        List<PlatServCategory> serviceCategoryList = platServCategoryService.listByPlat(plat);
        List<String> categIds = serviceCategoryList.stream().map(p -> p.getServCategId() + "-" + p.getCategOneId() + "-" + p.getCategId())
            .collect(Collectors.toList());
        if (servCategorys != null && servCategorys.size() > 0) {
            mav.addObject("currentServCategId", servCategorys.get(0).getKey());
        }

        mav.addObject("categIds", categIds);
        mav.addObject("servCategorys", servCategorys);
        mav.addObject("plat", plat);
        mav.addObject("items", logList);
        return mav;
    }
}
